<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-lx-cascades"></i> 俱乐部用户列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAll">批量禁用</el-button>
                <el-select v-model="select_cate" placeholder="筛选状态" class="handle-select mr10" @change="getData">
                    <el-option key="1" label="启用" value="0"></el-option>
                    <el-option key="2" label="禁用" value="1"></el-option>
                    <el-option key="3" label="全部" value="-1"></el-option>
                </el-select>
                <el-input v-model="select_word" placeholder="筛选关键词" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
            </div>
            <el-table :data="data" border class="table" ref="multipleTable" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column type="index" width="55" align="center" label="序列"></el-table-column>
                <el-table-column prop="scontent" label="晒图内容" width="auto">
                </el-table-column>
                <el-table-column prop="spic" label="晒图图片" width="120">
                    <template slot-scope="scope">
                        <el-image
                        fit="cover"
                        style="height:100px;width:100px;"
                        :src="'http://localhost/fitness/public/static/img/'+sharePic(scope.row,0)"
                        @click="handlePicPerview(scope.row)">
                        </el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="username" label="用户名" width="180">
                </el-table-column>
                <el-table-column prop="tel" label="晒图评论" width="100" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-document" @click="commentDetail(scope.row)">详情</el-button>
                    </template>
                </el-table-column>
                <el-table-column prop="sharetime" label="晒图时间" sortable width="200">
                </el-table-column>
                <el-table-column prop="sharestate" label="晒图状态" width="100" align="center">
                    <template slot-scope="scope">
                        <template v-if="scope.row.sharestate == 1">
                            <i class="el-icon-circle-close" style="color:red"> 已禁用</i>
                        </template>
                        <template v-else-if="scope.row.sharestate == 0">
                            <i class="el-icon-circle-check" style="color:#1e90ff"> 已通过</i>
                        </template>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="200" align="center">
                    <template slot-scope="scope">
                        <template v-if="scope.row.sharestate == 1">
                            <el-button type="text" icon="el-icon-circle-check" @click="handleOn(scope.row)">启用</el-button>
                        </template>
                        <template v-else-if="scope.row.sharestate == 0">
                            <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.row)">禁用</el-button>
                        </template>
                    </template>
                </el-table-column>
                
            </el-table>
        </div>
        <!-- 评论弹出框 -->
        <el-dialog title="评论详情" :visible.sync="dialogCommentVisible">
            <el-table :data="commentData">
                <el-table-column property="username" label="评论人" width="200"></el-table-column>
                <el-table-column property="content" label="评论内容"></el-table-column>
                <el-table-column property="commenttime" label="评论时间" align="center" width="160"></el-table-column>
                <el-table-column label="评论状态" width="100" align="center">
                    <template slot-scope="scope">
                        <template v-if="scope.row.commentstate == 0">
                            <i class="el-icon-circle-check" style="color:#1e90ff"> 通过</i>
                        </template>
                        <template v-else-if="scope.row.commentstate == 1">
                            <i class="el-icon-circle-close" style="color:red"> 禁用</i>
                        </template>
                        <template v-else-if="scope.row.commentstate == 2">
                            <i class="el-icon-circle-close" style="color:red"> 已删除</i>
                        </template>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="100" align="center">
                    <template slot-scope="scope">
                        <template v-if="scope.row.commentstate == 0">
                            <el-button type="text" class="red" @click="conComment(1,scope.row.commentid)">禁用</el-button>
                        </template>
                        <template v-else-if="scope.row.commentstate == 1">
                            <el-button type="text" class="color:#1e90ff" @click="conComment(0,scope.row.commentid)">启用</el-button>
                        </template>
                    </template>
                    
                </el-table-column>
            </el-table>
        </el-dialog>

        <!-- 删除提示框 -->
        <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
            <div class="del-dialog-cnt">是否确定禁用？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button type="primary" @click="deleteRow">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 启用提示框 -->
        <el-dialog title="提示" :visible.sync="onVisible" width="300px" center>
            <div class="del-dialog-cnt">是否确定启用？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="onVisible = false">取 消</el-button>
                <el-button type="primary" @click="onRow">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 图片弹出框 -->
        <el-dialog title="图片" :visible.sync="picVisible" width="35%" center>
            <div class="perview-card">
                <el-button icon="el-icon-arrow-left" circle @click="prePic"></el-button>
                <el-image 
                fit="cover"
                :src="'http://localhost/fitness/public/static/img/'+piclist[i]">
                </el-image>
                <el-button icon="el-icon-arrow-right" circle @click="nextPic"></el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    import { fetchData } from '../api/index';
    import { test } from '../api/index';
    export default {
        name: 'share',
        data() {
            return {
                tableData: [],
                cur_page: 1,
                multipleSelection: [],
                select_cate: '0',
                select_word: '',
                del_list: [],
                is_search: false,
                editVisible: false,
                delVisible: false,
                picVisible: false,
                onVisible: false,
                idx: -1,
                id: -1,
                dialogCommentVisible: false,
                commentData: [],
                visible: false,
                piclist:[],
                i:0
            }
        },
        created() {
            this.getData();
        },
        computed: {
            data() {
                return this.tableData.filter((d) => {
                    if ((d.scontent.indexOf(this.select_word) > -1)
                    ) {
                        return d;
                    }
                })
                // return this.tableData;
            }
        },
        methods: {
            // 分页导航
            handleCurrentChange(val) {
                this.cur_page = val;
                this.getData();
            },
            // 获取 easy-mock 的模拟数据
            getData() {
                this.$axios.get('/api/Share/shareList',{
                    params: {
                        mod: this.select_cate
                    }
                }).then(res => {
                    this.tableData = res.data;
                })
            },
            search() {
                this.is_search = true;
            },
            formatter(row, column) {
                return row.address;
            },
            filterTag(value, row) {
                return row.tag === value;
            },
            handleDelete(row) {
                this.id = row.shareid;
                this.delVisible = true;
            },
            handleOn(row){
                this.id = row.shareid;
                this.onVisible = true;
            },
            delAll() {
                const length = this.multipleSelection.length;
                let str = '';
                // this.del_list = this.del_list.concat(this.multipleSelection);
                for (let i = 0; i < length; i++) {
                    str += this.multipleSelection[i].scontent + ' ';
                    this.$axios.get('/api/Share/conShare',{
                        params: {
                            shareid: this.multipleSelection[i].shareid,
                            mod: 1
                        }
                    }).then(res => {
                        this.getData();
                    })
                }
                this.$message.error('删除了' + str);
                this.multipleSelection = [];
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 确定删除
            deleteRow(){
                this.delVisible = false;
                this.$axios.get('/api/Share/conShare',{
                    params: {
                        shareid: this.id,
                        mod: 1
                    }
                }).then(res => {
                    if(res.data == 1){
                        this.$message.success('禁用成功');
                        this.getData();
                    }
                })
            },
            // 确定启用
            onRow(){
                this.onVisible = false;
                this.$axios.get('/api/Share/conShare',{
                    params: {
                        shareid: this.id,
                        mod: 0
                    }
                }).then(res => {
                    if(res.data == 1){
                        this.$message.success('启用成功');
                        this.getData();
                    }
                })
            },
            commentDetail(row){
                this.commentData = [];
                this.dialogCommentVisible = true;
                this.id = row.shareid;
                this.$axios.get('/api/Share/shareComment',{
                    params: {
                        shareid: row.shareid
                    }
                }).then(res => {
                    this.commentData = res.data;
                })
            },
            conComment(mod,commentid){
                let temp = {shareid: this.id};
                this.$axios.get('/api/Share/conComment',{
                    params: {
                        mod: mod,
                        commentid: commentid
                    }
                }).then(res => {
                    if(res.data==1){
                        this.commentDetail(temp);
                    }
                })
            },
            sharePic(row,mod){
                // let piclist = JSON.parse(row.spic);
                switch (mod) {
                    case 0:
                        if(row.spic!=""){
                            return JSON.parse(row.spic)[0];
                        }
                        else
                            return ;
                        break;
                    case 1:
                        if(row.spic!=""){
                            let piclist = JSON.parse(row.spic);
                            Object.values(piclist).forEach(element => {
                                element = 'http://localhost/fitness/public/static/img/'+element;
                            });
                            return Object.values(piclist);
                        }
                        break;
                    default:
                        break;
                }

            },
            handlePicPerview(row){
                this.i = 0;
                if(row.spic!=""){
                    this.piclist = Object.values(JSON.parse(row.spic));
                }
                this.picVisible = true;
            },
            prePic(){
                let max = this.piclist.length-1;
                if(this.i==0){
                    this.i = max;
                }else{
                    this.i--;
                }
            },
            nextPic(){
                let max = this.piclist.length-1;
                if(this.i==max){
                    this.i=0
                }else{
                    this.i++;
                }
            }
        }
    }

</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .del-dialog-cnt{
        font-size: 16px;
        text-align: center
    }
    .table{
        width: 100%;
        font-size: 14px;
    }
    .red{
        color: #ff0000;
    }
    .mr10{
        margin-right: 10px;
    }
    .perview-card{
        align-items: center;
        justify-items: center;
        justify-content: center;
        display: flex;
        height: 300px;
        overflow: hidden;
        width: 100%;
    }
    .perview-card>.el-image{
        /* width: auto; */
        /* margin-left: 20px;
        margin-right: 20px; */
        height: auto;
        margin: 0 20px;
    }
</style>
